<template>
  <div class>
    <div class>用户管理-列表</div>
    <el-form
      size="mini"
      :inline="true"
      :model="searchForm"
      class="demo-form-inline search"
    >
      <el-form-item label="用户名">
        <el-input
          v-model="searchForm.realname"
          size="mini"
          placeholder="请输入内容"
        ></el-input>
      </el-form-item>
      <el-form-item label="组织" width="100">
        <el-select
          size="mini"
          v-model="searchForm.orgname"
          clearable
          placeholder="请选择"
        >
          <el-option value="wu"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="角色">
        <el-select
          size="mini"
          v-model="searchForm.role"
          clearable
          placeholder="请选择"
        >
          <el-option value="管理员"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="会员组">
        <el-select
          size="mini"
          v-model="searchForm.memberGroup"
          clearable
          placeholder="请选择"
        >
          <el-option value="wu"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="类型">
        <el-select
          size="mini"
          v-model="searchForm.type"
          clearable
          placeholder="请选择"
        >
          <el-option value="wu"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="状态">
        <el-select
          size="mini"
          v-model="searchForm.status"
          clearable
          placeholder="请选择"
        >
          <el-option value="审核"></el-option>
          <el-option value="待审核"></el-option>
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button size="mini" type="info" @click="Search">搜索</el-button>
      </el-form-item>
    </el-form>
    <div class="buttons">
      <el-button type="info" size="mini" @click="addUser">新增</el-button>
      <!-- <el-button type="info" size="mini">审核</el-button>
      <el-button type="info" size="mini">锁定</el-button>
      <el-button type="info" size="mini">解锁</el-button>
      <el-button type="info" size="mini">删除</el-button> -->
    </div>
    <el-table
      :data="userForm"
      style="width: 100%"
      :default-sort="{ prop: 'userid' }"
    >
      <el-table-column
        type="selection"
        label="全选"
        width="55"
      ></el-table-column>
      <el-table-column
        prop="userid"
        label="ID"
        sortable
        width="180"
        :default-sort="{ prop: 'userid' }"
      ></el-table-column>
      <el-table-column
        prop="realname"
        label="名称"
        width="180"
      ></el-table-column>
      <el-table-column
        prop="orgname"
        label="组织"
        width="180"
      ></el-table-column>
      <el-table-column prop="role" label="角色" width="180"></el-table-column>
      <el-table-column
        prop="memberGroup"
        label="会员组"
        width="180"
      ></el-table-column>
      <el-table-column prop="type" label="类型" width="180"></el-table-column>
      <el-table-column
        prop="grade"
        label="等级"
        sortable
        width="180"
      ></el-table-column>
      <el-table-column label="状态" width="180">
        <template slot-scope="scope">
          <span v-if="scope.row.status == 0">正常</span>
          <span v-if="scope.row.status == 1">待审核</span>
          <span v-if="scope.row.status == 2">锁定</span>
        </template>
      </el-table-column>
      <el-table-column fixed="right" label="操作" width="130">
        <template slot-scope="scope">
          <el-button
            v-if="scope.row.status == 2"
            @click="unlock(scope.row)"
            type="text"
            size="small"
            >解锁</el-button
          >
          <el-button
            v-if="scope.row.status == 1"
            type="text"
            size="small"
            @click="review(scope.row.userid)"
            >审核</el-button
          >
          <el-button
            v-if="scope.row.status == 0"
            @click="unlock(scope.row)"
            type="text"
            size="small"
            >锁定</el-button
          >
          <el-button @click="editUser" type="text" size="small">修改</el-button>
          <el-button type="text" size="small" @click="remove(scope.row.userid)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <div style="margin:0 auto;width:40%">
      <el-pagination
        @size-change="handleSize"
        @current-change="handlePage"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="10"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>
    </div>

    <Drawer :isDrawer.sync="isDrawer" :title="'用户管理-修改'" :size="'60%'">
      <Edit></Edit>
      <template #footer>
        <el-button size="medium" @click="closeDrawer">取消</el-button>
        <el-button type="primary" size="medium">保存</el-button>
      </template>
    </Drawer>
    <Drawer :isDrawer.sync="isDrawers" :title="'用户管理-新增'" :size="'60%'">
      <Add></Add>
      <template #footer>
        <el-button size="medium" @click="closeDrawer">取消</el-button>
        <el-button type="primary" size="medium">保存</el-button>
      </template>
    </Drawer>
  </div>
</template>

<script>
import * as api from "./api";
import Edit from "./components/userEdit";
import Add from "./components/userAdd";
import Drawer from "@/components/drawer";
export default {
  name: "",
  components: {
    Edit,
    Drawer,
    Add
  },
  data() {
    return {
      isDrawer: false, //修改弹出层显示隐藏
      isDrawers: false, //新增弹出层显示隐藏
      searchForm: {
        realname: "",
        orgname: "",
        role: "",
        memberGroup: "",
        type: "",
        status: ""
      },
      userForm: [
        {
          realname: "用户一",
          userid: 1,
          grade: "管理员"
        }
      ], //用户列表
      total: null, //总条数
      page: 1, //页数
      size: 10 //条数
    };
  },
  created() {
    this.userList();
  },
  methods: {
    // 获取用户列表
    userList() {
      const params = {
        page: this.page,
        limit: this.size
      };
      api.userList(params).then(res => {
        if (res.data.code == 200) {
          this.userForm = res.data.form.rows;
          this.total = res.data.form.total;
        }
      });
    },
    //搜索
    Search() {
      const params = this.searchForm;
      api.userList(params).then(res => {
        if (res.data.code == 200) {
          this.userForm = res.data.form.rows;
          this.total = res.data.form.total;
        }
      });
    },
    /**
     * 修改
     * 打开弹出层
     */
    editUser() {
      this.isDrawer = true;
    },
    /**
     * 新增
     * 打开弹出层
     */
    addUser() {
      this.isDrawers = true;
    },
    //解锁锁定
    unlock(row) {
      if (row.status == 0) {
        const params = {
          uid: row.userid,
          status: 2
        };
        api.lockList(params).then(res => {
          if (res.data.code == 200) {
            this.userList();
            this.$notify({
              title: "成功",
              message: "修改成功",
              type: "success"
            });
          }
        });
      } else {
        const params = {
          uid: row.userid,
          status: 0
        };
        api.lockList(params).then(res => {
          if (res.data.code == 200) {
            this.userList();
            this.$notify({
              title: "成功",
              message: "修改成功",
              type: "success"
            });
          }
        });
      }
    },
    //审核
    review(id) {
      api.reviewList({ uid: id }).then(res => {
        if (res.data.code == 200) {
          this.userList();
          this.$notify({
            title: "成功",
            message: "修改成功",
            type: "success"
          });
        }
      });
    },
    //删除
    remove(id) {
      api.delList({ uid: id }).then(res => {
        if (res.data.code == 200) {
          this.userList();
          this.$notify({
            title: "成功",
            message: "修改成功",
            type: "success"
          });
        }
      });
    },
    //关闭弹出层
    closeDrawer() {
      this.isDrawer = false;
      this.isDrawers = false;
    },
    //分页器-条数
    handleSize(val) {
      this.size = val;
      this.userList();
    },
    //分页器-页数
    handlePage(val) {
      this.page = val;
      this.userList();
    }
  }
};
</script>

<style lang="scss" scoped>
.search {
  background-color: #ffffff;
  border-radius: 5px;
  box-shadow: 0px 3px 6px rgba(0, 0, 0, 0.16);
  padding: 13px 10px 0;
  margin: 20px 0 30px;
  .el-select {
    width: 100px;
  }
}
.buttons {
  margin: 0 0 10px;
  .el-button {
    box-shadow: 0px 0px 6px rgba(0, 12, 100, 0.2);
  }
}
</style>
